from models import models
from app import db
from service import comm


def insert_release(**release_info):
    company_name = release_info['company_name']
    principal = release_info['principal']
    contact_number = release_info['contact_number']
    address = release_info['address']
    position = release_info['position']
    result = models.Release.query.filter_by(company_name=company_name, contact_number=contact_number).first()
    if result is None:
        release = models.Release(company_name=company_name, principal=principal, contact_number=contact_number, address=\
                                 address, position=position)
        db.session.add(release)
        db.session.commit()
        return release.id
    else:
        return result.id


def insert_agent(**agent_info):
    company_name = agent_info['company_name']
    principal = agent_info['principal']
    contact_number = agent_info['contact_number']
    address = agent_info['address']
    position = agent_info['position']
    result = models.Agent.query.filter_by(company_name=company_name, contact_number=contact_number).first()
    if result is None:
        agent = models.Agent(company_name=company_name, principal=principal, contact_number=contact_number, address=address, position=position)
        db.session.add(agent)
        db.session.commit()
        return agent.id
    else:
        return result.id


def insert_att(**att_info):
    file_type = att_info['file_type']
    file_url = att_info['file_url']
    result = models.Attachment.query.filter_by(file_url=file_url).first()
    if result is None:
        att = models.Attachment(file_type=file_type, file_url=file_url)
        db.session.add(att)
        db.session.commit()
        return att.id
    else:
        return result.id


def insert_bidder(**bidder_info):
    company_name = bidder_info['company_name']
    position = bidder_info['position']
    result = models.Attachment.query.filter_by(company_name=company_name).first()
    if result is None:
        bidder = models.Attachment(company_name=company_name, position=position)
        db.session.add(bidder)
        db.session.commit()
        return bidder.id
    else:
        return result.id


def insert_position(**position_info):
    province = position_info['position_info']
    city = position_info['city']
    county = position_info['county']
    result = models.Position.query.filter_by(city=city, county=county).first()
    if result is None:
        position = models.Position(province=province, city=city, county=county)
        db.session.add(position)
        db.session.commit()
        return position.id
    else:
        return result.id


def insert_tender(**tender_info):
    title = tender_info['title']
    url = tender_info['url']
    content = tender_info['content']
    release_id = tender_info['release_id']
    agent_release_id = tender_info['agent_release_id']
    attachment_id = tender_info['attachment_id']
    content_label = tender_info['content_label']
    release_time = tender_info['release_time']
    deadline = tender_info['deadline']
    types = tender_info['types']
    status = tender_info['status']
    bidder_id = tender_info['bidder_id']
    create_time = comm.get_cur_time()
    result = models.Tender.query.filter_by(url=url, title=title).first()
    if result is None:
        tender = models.Tender(title=title, url=url, content=content, release_id=release_id, agent_release_id=agent_release_id,
                               attachment_id=attachment_id, content_label=content_label, release_time=release_time, deadline=deadline,
                               types=types, status=status, bidder_id=bidder_id, create_time=create_time)
        db.session.add(tender)
        db.session.commit()
        return tender.id
    else:
        return result.id
